using GHIElectronics.NETMF.Glide.UI;

namespace Melody_Glider.GUIContent
{
    class CancelQuestionBox: WindowFrame
    {
        private readonly GUI _gui;
        private readonly SystemState _conditions;

        public CancelQuestionBox(GUI gui, SystemState state)
        {
            _gui = gui;
            _conditions = state;
        }

        protected override void InitButtons()
        {
            var saveBtn = (Button)ThisWindow.GetChildByName("SaveButton");
            saveBtn.TapEvent += SaveBtnTapEvent;

            var dontSaveBtn = (Button)ThisWindow.GetChildByName("DontSaveButton");
            dontSaveBtn.TapEvent += DontSaveBtnTapEvent;

            var cancelBtn = (Button)ThisWindow.GetChildByName("CancelButton");
            cancelBtn.TapEvent += CancelBtnTapEvent;
        }

        private void SaveBtnTapEvent(object sender)
        {   //todo: after NETMF 4.2
            //todo: encapsulate
            /*var path = _conditions.Drive.Volume.RootDirectory + @"\" + _conditions.ActiveFileName + ".tsm";
            using (var logStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                var fileContent = Encoding.UTF8.GetBytes(_conditions.MelodyBuffer.ToString());
                logStream.Write(fileContent, 0,fileContent.Length);
                logStream.Flush();
                logStream.Close();
            }*/
            BackToMainMenu();
        }

        private void DontSaveBtnTapEvent(object sender)
        {
            BackToMainMenu();
        }

        private void BackToMainMenu()
        {
            _gui.EditorWindow.DrawingStartIndex = 0;
            _conditions.SelectedNote = -1; //it can be moved to MainMenuWindow.NewBtnTapEvent I think, then it would also disappear from EditorWindow.SophisticatedInit
            _conditions.MelodyBuffer = null; //not neccesary because it won't be used again, but let it be free - or should this be disposed??? I have to examine the difference 'cause I probably miss something
            _conditions.Clipboard = null; //neccesary, or I may set it to null in SystemState.SetForNewFile()
            _gui.MainMenuWindow.SetToMain();
        }

        private void CancelBtnTapEvent(object sender)
        {
            _gui.EditorWindow.SetToMain();
        }
    }
}
